<?php include("../lib/functions.php"); ?>
<?php require_once('../../Connections/cotiza.php'); 
mysql_select_db($database_cotiza, $cotiza);
?>
<?php 
$acc   = $_GET['acc'];
$rut   = $_GET['rut'];
$campo = $_GET['campo'];

if($acc == "save")
{
	//Datos de cliente
	$rut       		= $_POST['rut'];
	$ad_addr        = $_POST['ad_addr'];
	$nombre 		= $_POST['nombre'];
	$direccion 		= $_POST['direccion'];
	$pais      		= strtoupper($_POST['pais']);
	$region    		= $_POST['region'];
	$comuna    		= $_POST['comuna'];
	$ciudad			= strtoupper($_POST['ciudad']);
	$campo          = $_POST['campo'];
	$nombre_cont    = $_POST['nombre_contacto'];
	$telefono       = $_POST['telefono'];
	$fax			= $_POST['fax'];
	//ad_name caben 28 caracteres
	
	$ad_ref = retorna_addr($cotiza,$rut);

	$query = "INSERT INTO ad_mstr (ad_addr, ad_ref, ad_name, ad_direccion, ad_pais, ad_ciudad, ad_region, ad_comuna, ad_nuevo) 
	                       VALUES ('$ad_addr','$ad_ref','$nombre','$direccion','$pais','$ciudad','$region','$comuna',1)";

	//echo $query;
	$ad = mysql_query($query,$cotiza);
	
	if($ad)
	{
		$path_ad   = "/u1/shares/cotizador/ad_mstr.csv";
		//$path_ad   = "c:\\ad_mstr.csv";

		if(strtoupper(substr($rut,0,2)) == "CE")
		{
			$cod = $rut;
			$rut = "55555555-7";
		}
		else
		{
			//$cod_array = explode("-",$rut);
			$cod = $ad_ref;
		}
		$ad = fopen($path_ad,'w+');
		fputs($ad,$cod.";");
		fputs($ad,$ad_addr.";");
		fputs($ad,$nombre.";");
		fputs($ad,$direccion.";");
		fputs($ad,$ciudad.";");
		fputs($ad,$comuna.";");
		fputs($ad,$region.";");
		fputs($ad,$pais.";");
		fputs($ad,$nombre_cont.";");
		fputs($ad,$telefono.";");
		fputs($ad,$fax.";");
		fputs($ad,$rut.";");
		fclose($ad);
	
		shell_exec("/u1/users/pfs/cotizador/load_cim2-1-13.sh");
		header("Location: ../browser/direcciones.php?id=$rut&campo=$campo");
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>...::: Agregando nueva direcci&oacute;n :::...</title>
<link href="../../main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../lib/libreriaAjax.js"></script>
<script type="text/javascript">
<?php if($bandera == 1){ ?>
	window.parent.SexyLightbox.close();
<?php }?>

function activa_button()
{
	var ad_addr   = document.getElementById('ad_addr').value;
	var dir       = document.getElementById('direccion').value;
	var region    = document.getElementById('region').value;
	var comuna    = document.getElementById('comuna').value;
	
	if(ad_addr != "" && dir != "" && region != "" && comuna != "")
		document.getElementById('guardar').disabled = false
	else
		document.getElementById('guardar').disabled = true
}
</script>
<style type="text/css">
<!--
body {
	margin-top: 0px;
}
-->
</style></head>

<body onload="document.getElementById('nombre').focus();">
<strong><br />
<br />
</strong>
<form id="form1" name="form1" method="post" action="mntodirecciones.php?acc=save">
  <table border="0" align="center">
    <tr>
      <td colspan="4"><strong>Datos de la nueva direcci&oacute;n</strong></td>
    </tr>
    <tr>
      <td width="90">Rut Cliente</td>
      <td width="4">:</td>
      <td width="62"><input name="rut" type="text" class="default" id="rut" size="10" maxlength="10" autocomplete="off" onkeyup="activa_button()" readonly="readonly" value="<?php echo $rut;?>"/></td>
      <td width="302" class="detail">Sin puntos (Ej. 96634140-8)
      <input type="hidden" name="campo" id="campo" value="<?php echo $campo;?>" /></td>
    </tr>
    <tr>
      <td>C&oacute;digo direcci&oacute;n</td>
      <td>:</td>
      <td colspan="2"><input name="ad_addr" type="text" class="default" id="ad_addr" onkeyup="activa_button()" size="8" maxlength="8" readonly="readonly" value="<?php echo asigna_addr($cotiza,7);?>"/></td>
    </tr>
    <tr>
      <td>Nombre</td>
      <td>:</td>
      <td colspan="2"><input  name="nombre" type="text" class="default" id="nombre" size="50" autocomplete="off" onkeyup="activa_button()"/></td>
    </tr>
    <tr>
      <td>Direcci&oacute;n</td>
      <td>:</td>
      <td colspan="2"><input  name="direccion" type="text" class="default" id="direccion" size="50" autocomplete="off" onkeyup="activa_button()"/></td>
    </tr>
    <tr>
      <td>Pa&iacute;s</td>
      <td>:</td>
      <td colspan="2"><select name="pais" class="default" id="pais">
        <?php 
			$query     = "SELECT * FROM ctry_mstr ORDER BY ctry_country";
			$rslt_ctry = mysql_query($query,$cotiza);
			$row_ctry  = mysql_fetch_assoc($rslt_ctry);
			do{
		?>
        <option value="<?php echo $row_ctry['ctry_code'];?>" <?php if($row_ctry['ctry_code'] == "CHL") echo "selected=\"selected\"";?>  ><?php echo $row_ctry['ctry_country'];?></option>
        <?php }while($row_ctry  = mysql_fetch_assoc($rslt_ctry))?>
      </select></td>
    </tr>
    <tr>
      <td>Regi&oacute;n</td>
      <td>:</td>
      <td colspan="2"><select name="region" class="default" id="region" onchange="textoAjax('../auxiliar/comunas.php?name=comuna&rg='+this.value,'comuna-div');activa_button();">
	    <option value="" selected="selected">Seleccione una Regi&oacute;n...</option>
      <?php
		$query = "SELECT * FROM reg_mstr";
		$rst   =  mysql_query($query,$cotiza) or die(mysql_error());
		while($fila=mysql_fetch_assoc($rst)){
	  ?>
        <option value="<?php echo $fila['reg_id']?>"><?php echo $fila['reg_desc']?></option>
        <?php }?>
      </select></td>
    </tr>
    <tr>
      <td>Comuna</td>
      <td>:</td>
      <td colspan="2"><div id="comuna-div"><select name="comuna" class="default" id="comuna" disabled="disabled">
        <option>Seleccione una Regi&oacute;n...</option>
      </select></div></td>
    </tr>
    <tr>
      <td>Ciudad</td>
      <td>:</td>
      <td colspan="2"><input name="ciudad" type="text" class="default" id="ciudad" autocomplete="off" onkeyup="activa_button()"/></td>
    </tr>
  </table>
  <br />
  <table border="0" align="center">
    <tr>
      <td colspan="3"><strong>Datos del nuevo contacto</strong></td>
    </tr>
    <tr>
      <td width="90" height="19">Nombre contacto</td>
      <td width="4">:</td>
      <td width="368"><input name="nombre_contacto" type="text" class="default" id="nombre_contacto" onkeyup="activa_button()" size="30" autocomplete="off"/></td>
    </tr>
    <tr>
      <td>Tel&eacute;fono</td>
      <td>:</td>
      <td><input name="telefono" type="text" class="default" id="telefono" onkeyup="activa_button()" autocomplete="off"/></td>
    </tr>
    <tr>
      <td>Fax</td>
      <td>:</td>
      <td><input  name="fax" type="text" class="default" id="fax" autocomplete="off" onkeyup="activa_button()"/></td>
    </tr>
  </table>
  <br />
  <br />
  <table width="405" border="0" align="center">
    <tr>
      <td width="340" align="right"><input name="volver" type="button" class="default" id="volver" onclick="window.open('../browser/direcciones.php?id=<?php echo $rut;?>&campo=<?php echo $campo; ?>','_self')" value="Volver"/></td>
      <td width="55" align="right"><input name="guardar" type="button" class="default" id="guardar" onclick="document.forms.form1.submit()" value="Guardar" disabled="disabled"/></td>
    </tr>
  </table>
</form>
</body>
</html>
<?php 
mysql_close($cotiza);
?>